#! /bin/sh
#
# This script changes the file name extension for all C++ files
# from .cc to a different extension. This is useful for C++ compilers
# which do note recognise ".cc" as a valid extension for C++ code.
# For instance, the IBM xlC compiler on AIX 3.x requires ".C"
# and Microsofts Visual C++ needs ".cpp" or ".cxx"
#
# The script must be called from the "dcmtk/" directory, e.g.
#   cd dcmtk
#   config/changext cpp
#
# The first command line argument (if given) overrides the
#   new extension (default: C). 
#   You must not specify the dot preceding the extension.
#
# The second command line argument (if given) overrides the
#   old extension (default: cc). This is useful if you want
#   to "rename back".
#
# Author: Marco Eichelberg, (C) 1997-2005 Kuratorium OFFIS e.V.
#
 
newext=${1-C}
oldext=${2-cc}

echo "renaming C++ files from .$oldext to .$newext"
for file in `find . -name "*.$oldext" -print`
do
  newfile=`echo $file | sed -e "s/\.$oldext/\.$newext/g"`
  `mv $file $newfile`
done

if [ -f dcmjpeg/apps/dcmj2pnm.$newext ] ; then
  echo "updating implementation include in dcmj2pnm.$newext"
  `cat dcmjpeg/apps/dcmj2pnm.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmimage\/apps\/dcm2pnm\.$oldext\"/#include \"..\/..\/dcmimage\/apps\/dcm2pnm.$newext\"/g" >dcmjpeg/apps/dcmj2pnm.new`
  `mv dcmjpeg/apps/dcmj2pnm.new dcmjpeg/apps/dcmj2pnm.$newext`
fi

if [ -f dcmjpeg/apps/dcmmkdir.$newext ] ; then
  echo "updating implementation include in dcmmkdir.$newext"
  `cat dcmjpeg/apps/dcmmkdir.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmdata\/apps\/dcmgpdir\.$oldext\"/#include \"..\/..\/dcmdata\/apps\/dcmgpdir.$newext\"/g" >dcmjpeg/apps/dcmmkdir.new`
  `mv dcmjpeg/apps/dcmmkdir.new dcmjpeg/apps/dcmmkdir.$newext`
fi

if [ -f dcmmisc/apps/tcpsrv_e.$newext ] ; then
  echo "updating implementation include in tcpsrv_e.$newext"
  `cat dcmmisc/apps/tcpsrv_e.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmprint\/apps\/tcpsrv\.$oldext\"/#include \"..\/..\/dcmprint\/apps\/tcpsrv.$newext\"/g" >dcmmisc/apps/tcpsrv_e.new`
  `mv dcmmisc/apps/tcpsrv_e.new dcmmisc/apps/tcpsrv_e.$newext`
fi

if [ -f dcmmisc/apps/tcpprt_e.$newext ] ; then
  echo "updating implementation include in tcpprt_e.$newext"
  `cat dcmmisc/apps/tcpprt_e.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmprint\/apps\/tcpprt\.$oldext\"/#include \"..\/..\/dcmprint\/apps\/tcpprt.$newext\"/g" >dcmmisc/apps/tcpprt_e.new`
  `mv dcmmisc/apps/tcpprt_e.new dcmmisc/apps/tcpprt_e.$newext`
fi

if [ -f dcmmisc/apps/dcmcjp2e.$newext ] ; then
  echo "updating implementation include in dcmcjp2e.$newext"
  `cat dcmmisc/apps/dcmcjp2e.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmjp2k\/apps\/dcmcjp2k\.$oldext\"/#include \"..\/..\/dcmjp2k\/apps\/dcmcjp2k.$newext\"/g" >dcmmisc/apps/dcmcjp2e.new`
  `mv dcmmisc/apps/dcmcjp2e.new dcmmisc/apps/dcmcjp2e.$newext`
fi

if [ -f dcmmisc/apps/dcmdjp2e.$newext ] ; then
  echo "updating implementation include in dcmdjp2e.$newext"
  `cat dcmmisc/apps/dcmdjp2e.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmjp2k\/apps\/dcmdjp2k\.$oldext\"/#include \"..\/..\/dcmjp2k\/apps\/dcmdjp2k.$newext\"/g" >dcmmisc/apps/dcmdjp2e.new`
  `mv dcmmisc/apps/dcmdjp2e.new dcmmisc/apps/dcmdjp2e.$newext`
fi

if [ -f dcmqrdbx/apps/dcmqrscq.$newext ] ; then
  echo "updating implementation include in dcmqrscq.$newext"
  `cat dcmqrdbx/apps/dcmqrscq.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmqrdb\/apps\/dcmqrscp\.$oldext\"/#include \"..\/..\/dcmqrdb\/apps\/dcmqrscp.$newext\"/g" >dcmqrdbx/apps/dcmqrscq.new`
  `mv dcmqrdbx/apps/dcmqrscq.new dcmqrdbx/apps/dcmqrscq.$newext`
fi

echo "updating makefiles"
for file in `find . -name "Makefile*" -print`
do
  `cat $file | sed -e "s/\.$oldext/\.$newext/g" >$file.new`
  `mv $file.new $file`
done

echo "done."
